package www.worq.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import www.worq.controller.beans.faqBean;

public class faqDao extends oreqDao {

	public ArrayList<faqBean> getFAQS() throws SQLException {
		ArrayList<faqBean> faqs = new ArrayList<faqBean>();
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select * from faqs");

			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();

			System.out.println("dao.faqs.getFAQS.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				faqBean faq = new faqBean();
				faq.setId_faq(rs.getInt(1));
				faq.setPregunta(rs.getString(2));
				faq.setRespuesta(rs.getString(3));
				faqs.add(faq);
			}
			System.out.println("dao.faqs.getFAQS----------> Cargado");
		} catch (Exception e) {
			System.out.println("faqsDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return faqs;
	}

	public int nextId() {
		int id = 0;
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select id_faq from faqs");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.faqs.nextId.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				if (id < rs.getInt(1)) {
					id = rs.getInt(1);
				}
			}
			System.out.println("dao.faqs.nextId----------> Cargado");
		} catch (Exception e) {
			System.out.println("faqsDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return id + 1;
	}

	public void insertFAQ(faqBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("INSERT INTO faqs ")
					.append("(id_faq, pregunta, respuesta) ")
					.append("values ('").append(bean.getId_faq()).append("','")
					.append(bean.getPregunta()).append("','")
					.append(bean.getRespuesta()).append("');");

			System.out.println("dao.faqs.insertFAQ.Ejecutado----------> "
					+ query.toString());
			st = conn.createStatement();
			st.executeUpdate(query.toString());

			System.out.println("dao.faqs.insertFAQ----------> insertado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void deleteFAQ(faqBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("DELETE FROM faqs where id_faq=").append(
					bean.getId_faq());

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out.println("dao.faqs.deleteFAQ.Ejecutado----------> "
					+ query.toString());
			System.out.println("dao.faqs.deleteFAQ----------> eliminado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void updateFAQ(faqBean bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("UPDATE faqs SET ").append("pregunta='")
					.append(bean.getPregunta()).append("',respuesta='")
					.append(bean.getRespuesta()).append("' where id_faq=")
					.append(bean.getId_faq()).append(";");

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out.println("dao.faqs.updateFAQ.Ejecutado----------> "
					+ query.toString());
			System.out.println("dao.faqs.updateFAQ----------> actualizado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}
}
